# -*- coding: utf-8 -*-
# @Time    : 2020/6/1 11:19
# @Author  : suhong
# @File    : year_month_count.py
# @Software: PyCharm
import time

from re_common.baselibrary.utils.basedir import BaseDir
from re_common.baselibrary.utils.basefile import BaseFile
from re_common.facade.mysqlfacade import MysqlUtiles
from re_common.baselibrary import IniConfig
from re_common.facade.loggerfacade import get_streamlogger


class Record_Count(object):
    def __init__(self):
        self.curPath = BaseDir.get_file_dir_absolute(__file__)
        self.ini_message = IniConfig(BaseFile.get_new_path(self.curPath, "config.ini")).set_keep_keys_case(
            False).builder().get_cf()
        self.mysqls = MysqlUtiles(BaseFile.get_new_path(self.curPath, "config.ini"), "db", get_streamlogger())

        self.year_list = self.ini_message["year_month_argument"]["year"]
        self.month_list = self.ini_message["year_month_argument"]["month"]

    def year_month(self):
        outfilepath = './{}--{}.txt'.format(self.year_list, self.month_list)
        # 一月份需要全部数据，二月份以后只需要维普数据
        taks__sql = "select provider from task where who like '%维普+%'and if_active = 1"
        bool, task_rows = self.mysqls.SelectFromDB(taks__sql)
        for year in eval(self.year_list):
            for month in eval(self.month_list):
                print(month)
                line = "1月以后只算维普\n"
                ziku = ziku_add = tilu = quanwen = yinwei = cover = tilu_add = quanwen_add = yinwei_add = cover_add = 0
                date = '{}-{}-1'.format(year, month)
                date_n = '{}-{}-1'.format(year, int(month) + 1)
                if year == 2020 and month == 1:
                    # 子库增量
                    sql = "select count(*) from task WHERE create_time between '%s' and '%s'" % (date, date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    ziku_add = rows[0][0]
                    # 子库总量
                    sql = "select count(*) from task WHERE create_time < '%s'" % (date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    ziku = rows[0][0]

                    # 题录增量
                    sql = "select sum(increment_metadata) FROM updating WHERE update_date between '%s' and '%s'" % (
                        date, date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    tilu_add = rows[0][0]
                    # 题录总量
                    sql = "select max(count_metadata) FROM updating WHERE update_date < '%s' GROUP BY provider" % (
                        date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    for row in rows:
                        tilu += int(row[0])

                    # 全文增量
                    sql = "select sum(increment_fulltext) FROM updating WHERE update_date between '%s' and '%s'" % (
                        date, date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    quanwen_add = rows[0][0]
                    # 全文总量
                    sql = "select max(count_fulltext) FROM updating WHERE update_date < '%s' GROUP BY provider" % (
                        date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    for row in rows:
                        quanwen += int(row[0])

                    # 引文增量
                    sql = "select sum(increment_ref) FROM updating WHERE update_date between '%s' and '%s'" % (
                        date, date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    yinwei_add = rows[0][0]
                    # 引文总量
                    sql = "select max(count_ref) FROM updating WHERE update_date < '%s' GROUP BY provider" % (date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    for row in rows:
                        yinwei += int(row[0])

                    # 封面增量
                    sql = "select sum(increment_cover) FROM updating WHERE update_date between '%s' and '%s'" % (
                        date, date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    cover_add = rows[0][0]
                    # 封面总量
                    sql = "select max(count_cover) FROM updating WHERE  update_date < '%s' GROUP BY provider" % (date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    for row in rows:
                        cover += int(row[0])
                else:
                    # 子库增量
                    sql = "select count(*) from task WHERE who like '%维普+%' and create_time between '{}' and '{}'".format(
                        date, date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    ziku_add = rows[0][0]
                    # 子库总量
                    sql = "select count(*) from task where who like '%维普+%' and create_time < '{}'".format(date_n)
                    bool, rows = self.mysqls.SelectFromDB(sql)
                    ziku = rows[0][0]

                    # 二月份以后就只取维普

                    for task in task_rows:
                        info = task[0]
                        # 题录增量
                        sql = "select sum(increment_metadata) FROM updating WHERE provider = '%s' and update_date between '%s' and '%s'" % (
                            info, date, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        if not rows[0][0] is None:
                            tilu_add += int(rows[0][0])
                        # 题录总量
                        sql = "select max(count_metadata) FROM updating WHERE provider = '%s' and update_date < '%s' GROUP BY provider" % (
                            info, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        for row in rows:
                            tilu += int(row[0])

                        # 全文增量
                        sql = "select sum(increment_fulltext) FROM updating WHERE provider = '%s' and  update_date between '%s' and '%s'" % (
                            info, date, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        if not rows[0][0] is None:
                            quanwen_add += int(rows[0][0])
                        # 全文总量
                        sql = "select max(count_fulltext) FROM updating WHERE provider = '%s' and update_date < '%s' GROUP BY provider" % (
                            info, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        for row in rows:
                            quanwen += int(row[0])

                        # 引文增量
                        sql = "select sum(increment_ref) FROM updating WHERE provider = '%s' and update_date between '%s' and '%s'" % (
                            info, date, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        if not rows[0][0] is None:
                            yinwei_add += int(rows[0][0])
                        # 引文总量
                        sql = "select max(count_ref) FROM updating WHERE provider = '%s' and update_date < '%s' GROUP BY provider" % (
                            info, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        for row in rows:
                            yinwei += int(row[0])

                        # 封面增量
                        sql = "select sum(increment_cover) FROM updating WHERE provider = '%s' and update_date between '%s' and '%s'" % (
                            info, date, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        if not rows[0][0] is None:
                            cover_add += int(rows[0][0])
                        # 封面总量
                        sql = "select max(count_cover) FROM updating WHERE provider = '%s' and  update_date < '%s' GROUP BY provider" % (
                            info, date_n)
                        bool, rows = self.mysqls.SelectFromDB(sql)
                        for row in rows:
                            cover += int(row[0])

                line += "年份：" + str(year)
                line += "月份：" + str(month) + "\n"
                line += "子库增量：" + str(ziku_add) + "\n"
                line += "子库总量：" + str(ziku) + "\n"
                line += "题录增量：" + str(tilu_add) + "\n"
                line += "题录总量：" + str(tilu) + "\n"
                line += "全文增量：" + str(quanwen_add) + "\n"
                line += "全文总量：" + str(quanwen) + "\n"
                line += "引文增量：" + str(yinwei_add) + "\n"
                line += "引文总量：" + str(yinwei) + "\n"
                line += "封面增量：" + str(cover_add) + "\n"
                line += "封面总量：" + str(cover) + "\n"
                line += "************************" + "\n"

                BaseFile.single_add_file(outfilepath, line)


    def year_month_2(self):
        # outfilepath = './{}--{}.txt'.format(self.year_list, self.month_list)
        outfilepath = './{}--123.txt'.format(self.year_list)
        # 一月份需要全部数据，二月份以后只需要维普数据
        taks__sql = "select provider from task where who like '%维普+%'and if_active = 1"
        bool, task_rows = self.mysqls.SelectFromDB(taks__sql)

        # # 子库增量
        # sql = "select count(*) from task WHERE who like '%维普+%' and YEAR(create_time) = '{}'".format(
        #     2020)
        # bool, rows = self.mysqls.SelectFromDB(sql)
        # ziku_add = rows[0][0]
        # # 子库总量
        # sql = "select count(*) from task where who like '%维普+%' and YEAR(create_time) <'{}'".format(2020)
        # bool, rows = self.mysqls.SelectFromDB(sql)
        # ziku = rows[0][0]
        # print(ziku_add,ziku)
        for year in eval(self.year_list):

            ziku = ziku_add = tilu = quanwen = yinwei = cover = tilu_add = quanwen_add = yinwei_add = cover_add = 0
            line = ""
            line1 = ""

            for task in task_rows:
                info = task[0]
                # 题录增量
                sql = "select sum(increment_metadata) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s'" % (
                    info,year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    tilu_add += int(rows[0][0])
                # 题录总量
                sql = "select max(count_metadata) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    tilu += int(row[0])

                # 全文增量
                sql = "select sum(increment_fulltext) FROM updating WHERE provider = '%s' and  YEAR(update_date)  = '%s'" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    quanwen_add += int(rows[0][0])
                    # if int(rows[0][0]) > 0:
                    #     line1 += "{}-----{} \n".format(info, int(rows[0][0]))
                # 全文总量
                sql = "select max(count_fulltext) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    quanwen += int(row[0])

                    line1 += "{}-----{} \n".format(info, int(row[0]))



                # 引文增量
                sql = "select sum(increment_ref) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s'" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    yinwei_add += int(rows[0][0])
                # 引文总量
                sql = "select max(count_ref) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    yinwei += int(row[0])

                # 封面增量
                sql = "select sum(increment_cover) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s'" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    cover_add += int(rows[0][0])

                # 封面总量
                sql = "select max(count_cover) FROM updating WHERE provider = '%s' and  YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    cover += int(row[0])

                    # line += "封面:{} ----- {} \n".format(info, int(row[0]))


        line += "年份：" + str(year) + "\n"
        line += "子库增量：" + str(ziku_add) + "\n"
        line += "子库总量：" + str(ziku) + "\n"
        line += "题录增量：" + str(tilu_add) + "\n"
        line += "题录总量：" + str(tilu) + "\n"
        line += "全文增量：" + str(quanwen_add) + "\n"
        line += "全文总量：" + str(quanwen) + "\n"
        line += "引文增量：" + str(yinwei_add) + "\n"
        line += "引文总量：" + str(yinwei) + "\n"
        line += "封面增量：" + str(cover_add) + "\n"
        line += "封面总量：" + str(cover) + "\n"
        line += "************************" + "\n"

        BaseFile.single_add_file(outfilepath, line)
        # BaseFile.single_add_file("./test.txt", line1)


    def year_month_3(self):
        # outfilepath = './{}--{}.txt'.format(self.year_list, self.month_list)
        outfilepath = './{}--3.txt'.format(self.year_list)
        # 一月份需要全部数据，二月份以后只需要维普数据
        taks__sql = "select provider,provider_popular from task where who like '%维普+%'and if_active = 1"
        bool, task_rows = self.mysqls.SelectFromDB(taks__sql)

        # # 子库增量
        # sql = "select count(*) from task WHERE who like '%维普+%' and YEAR(create_time) = '{}'".format(
        #     2020)
        # bool, rows = self.mysqls.SelectFromDB(sql)
        # ziku_add = rows[0][0]
        # # 子库总量
        # sql = "select count(*) from task where who like '%维普+%' and YEAR(create_time) <'{}'".format(2020)
        # bool, rows = self.mysqls.SelectFromDB(sql)
        # ziku = rows[0][0]
        # print(ziku_add,ziku)
        for year in eval(self.year_list):

            ziku = ziku_add = tilu = quanwen = yinwei = cover = tilu_add = quanwen_add = yinwei_add = cover_add = 0
            line = ""
            line1 = ""

            for task in task_rows:
                info = task[0]
                chinese = task[1]
                if "journallist" in info:
                    continue
                line1 = info + "(" + chinese + "):" + "\n"
                # 题录增量
                sql = "select sum(increment_metadata) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s'" % (
                    info,year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    tilu_add += int(rows[0][0])
                    line1 += "题录增量:" + str(rows[0][0]) + "\n"
                # 题录总量
                sql = "select max(count_metadata) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    tilu += int(row[0])

                # 全文增量
                sql = "select sum(increment_fulltext) FROM updating WHERE provider = '%s' and  YEAR(update_date)  = '%s'" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    quanwen_add += int(rows[0][0])
                    line1 += "全文增量:" + str(rows[0][0]) + "\n"
                    # if int(rows[0][0]) > 0:
                    #     line1 += "{}-----{} \n".format(info, int(rows[0][0]))
                # 全文总量
                sql = "select max(count_fulltext) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    quanwen += int(row[0])

                    # line1 += "{}-----{} \n".format(info, int(row[0]))



                # 引文增量
                sql = "select sum(increment_ref) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s'" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    yinwei_add += int(rows[0][0])
                    line1 += "引文增量:" + str(rows[0][0]) + "\n"
                # 引文总量
                sql = "select max(count_ref) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    yinwei += int(row[0])

                # 封面增量
                sql = "select sum(increment_cover) FROM updating WHERE provider = '%s' and YEAR(update_date)  = '%s'" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                if not rows[0][0] is None:
                    cover_add += int(rows[0][0])
                    line1 += "封面增量:" + str(rows[0][0]) + "\n"

                # 封面总量
                sql = "select max(count_cover) FROM updating WHERE provider = '%s' and  YEAR(update_date)  = '%s' GROUP BY provider" % (
                    info, year)
                bool, rows = self.mysqls.SelectFromDB(sql)
                for row in rows:
                    cover += int(row[0])

                    # line += "封面:{} ----- {} \n".format(info, int(row[0]))
                BaseFile.single_add_file("./test.txt", line1)

        # line += "年份：" + str(year) + "\n"
        # line += "子库增量：" + str(ziku_add) + "\n"
        # line += "子库总量：" + str(ziku) + "\n"
        # line += "题录增量：" + str(tilu_add) + "\n"
        # line += "题录总量：" + str(tilu) + "\n"
        # line += "全文增量：" + str(quanwen_add) + "\n"
        # line += "全文总量：" + str(quanwen) + "\n"
        # line += "引文增量：" + str(yinwei_add) + "\n"
        # line += "引文总量：" + str(yinwei) + "\n"
        # line += "封面增量：" + str(cover_add) + "\n"
        # line += "封面总量：" + str(cover) + "\n"
        # line += "************************" + "\n"

        # BaseFile.single_add_file(outfilepath, line)


if __name__ == '__main__':
    r = Record_Count()
    r.year_month_3()
